package com.hy;

import java.util.ArrayList;
import java.util.List;

/**
 * Created With IntelliJ IDEA.
 * Descriptions:
 * author: Mr.Du
 * Date: 2023/10/18
 * Time: 10:02
 */
public class ReadBinaryWatch {

    public List<String> readBinaryWatch(int turnedOn) {
        List<String> ans = new ArrayList<String>();
        for (int h = 0; h < 12; ++h) {
            for (int m = 0; m < 60; ++m) {
                // 只要时分二进制1的个数相加等于turnedOn即可
                // if (countOne(h) + countOne(m) == turnedOn)
                if (Integer.bitCount(h) + Integer.bitCount(m) == turnedOn){
                    ans.add(h + ":" + (m < 10 ? "0" : "") + m);
                }
            }
        }
        return ans;
    }

    public int countOne(int m){
        int res = 0;
        while(m > 0){
            if((m & 1) == 1){
                res++;
            }
            m /= 2;
        }
        return res;
    }
}
